﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using KingdeePdaService;
using System.IO;

namespace SmartDeviceProject9
{
    // KingdeePdaService.PDADictionary<string,string>
    public partial class LoginForm : Form
    {

       

        public LoginForm()
        {
                 InitializeComponent();
                Rebind();
                tbxIP_LostFocus(null, null);

                lastIp = this.tbxIP.Text.Trim();
                lastPort = this.tbxPort.Text.Trim();

        }
        private readonly string configMsgNmae = "log1";

     

        private string AccountId;

        private void Rebind() {
            if (File.Exists(configMsgNmae))
            {
                using (StreamReader rd = new StreamReader(configMsgNmae, System.Text.Encoding.UTF8))
                {

                    while (rd.Peek() != -1)
                    {

                        string s = rd.ReadLine();
                        if (s.StartsWith("ip:"))
                        {
                            this.tbxIP.Text = s.Substring(s.IndexOf(":") + 1);
                        }
                        else if (s.StartsWith("port:"))
                        {
                            this.tbxPort.Text = s.Substring(s.IndexOf(":") + 1);
                        }
                        else if (s.StartsWith("accountid:"))
                        {
                            AccountId = s.Substring(s.IndexOf(":") + 1);
                        }
                        else if (s.StartsWith("username:"))
                        {
                            this.tbxUserName.Text = s.Substring(s.IndexOf(":") + 1);
                        }
                        else if (s.StartsWith("password:"))
                        {

                            this.tbxPassword.Text = s.Substring(s.IndexOf(":") + 1);

                            this.cbxRemberPwd.Checked = true;
                        }

                    }

                }
            }
        }

        private void Remember() {
         
                using (StreamWriter wt = new StreamWriter(configMsgNmae, false, System.Text.Encoding.UTF8))
                {

                    wt.WriteLine("ip:" + this.tbxIP.Text);
                    wt.WriteLine("port:" + this.tbxPort.Text);
                    wt.WriteLine("accountid:" + this.cbxAccounts.SelectedValue);
                    wt.WriteLine("username:" + this.tbxUserName.Text);
                    if (this.cbxRemberPwd.Checked)
                        wt.WriteLine("password:" + this.tbxPassword.Text);
                    wt.Flush();
                    wt.Close();
                }
            
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //cbxAccounts.SelectedValue

            string v =this.cbxAccounts.SelectedValue as string;
            string userName = this.tbxUserName.Text.Trim();
            string pwd = this.tbxPassword.Text.Trim();
            if (string.IsNullOrEmpty(v))
            {
                MessageBox.Show("请选择账套");
               
                return;
            
            }
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("请输入用户名");

                return;

            }

            if (string.IsNullOrEmpty(pwd))
            {
                MessageBox.Show("请输入密码");

                return;

            }

            DataSet loginSet = Context.Service.Login(v, userName, pwd, Context.LocalId);
            if (loginSet==null)
            {

                MessageBox.Show("用户名密码错误");

            }
            else
            {
                Remember();
                //MessageBox.Show("登录成功");
                Context.UserName = userName;
                Context.Password = pwd;
                Context.AccountId =v;
                DataTable loginTable = loginSet.Tables[0];
                DataRow loginRow=loginTable.Rows[0];
                Context.UserCurrentOrgName = Convert.ToString(loginRow["OrgName"]);
                Context.UserCurrentOrgId = Convert.ToString(loginRow["OrgId"]);
                SelectEntrys sf = new SelectEntrys();
                sf.Show();
                this.Hide();
                //获取组织及仓库
                DataSet data = Context.Service.GetOrgStock(Context.AccountId, Context.UserName, Context.Password, Context.LocalId);
                Org org = new Org();
                DataTable table = data.Tables[0];

                Dictionary<string, Org> orgs = new Dictionary<string, Org>();
                string on;
                foreach (DataRow dr in table.Rows)
                {
                    on = Convert.ToString(dr["FUseOrgId"]);
                    Org og = null;
                    Stk stk = new Stk();
                    stk.Name = Convert.ToString(dr["StockFName"]);
                    stk.Number = Convert.ToString(dr["StockFNumber"]);
                    stk.StkId = Convert.ToString(dr["StockId"]);
                    if (!orgs.TryGetValue(on, out og))
                    {
                        og = new Org();
                        og.Number = Convert.ToString(dr["OrgFNumber"]);
                        og.Name = Convert.ToString(dr["OrgFName"]);
                        og.OrgId = on;
                        og._stks = new List<Stk>();
                        og._stks.Add(stk);
                        orgs.Add(on, og);
                    }
                    else
                    {
                        orgs[on]._stks.Add(stk);
                    }
                }
                Context.Orgs = orgs;
                Dictionary<string, string> machine = new Dictionary<string, string>();
                machine.Add("1", "A机-面");
                machine.Add("2", "A机-瓦");

                machine.Add("3", "B机-面");
                machine.Add("4", "B机-瓦");

                machine.Add("5", "E机-面");
                machine.Add("6", "E机-瓦");

                machine.Add("7", "复合机");
                Context.Machines = machine;
            
            }

            
            //PdaServer.Service1 s = new SmartDeviceProject9.PdaServer.Service1();
            //MessageBox.Show(s.HelloWorld());
            //System.Data.
           

        }

        private void tbxIP_LostFocus(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.tbxIP.Text) && !string.IsNullOrEmpty(this.tbxPort.Text)) {
                if (this.tbxIP.Text.Trim() != lastIp || this.tbxPort.Text.Trim() != lastPort)
                {

                    LoadAccountMsg();
                   lastIp= this.tbxIP.Text.Trim() ;
                   lastPort = this.tbxPort.Text.Trim();
                }
            }    
        }
        string lastIp = string.Empty;
        string lastPort = string.Empty;
        private void LoadAccountMsg() {

            if (cbxAccounts.Items.Count > 0)
            {
                cbxAccounts.DataSource = null;
               // cbxAccounts.Items.Clear();

            }
            Context.Service = new SmartDeviceProject9.Server.PdaService();
            Context.Service.Url = "http://" + this.tbxIP.Text.Trim() + ":" + this.tbxPort.Text.Trim() + "/PdaService.asmx";

            try
            {
              System.Data.DataSet dts= Context.Service.GetAccounts();
              DataTable tabl = dts.Tables[0];
                //tabl.Columns.Add(new DataColumn("accountText", typeof(string)));
               // tabl.Columns.Add(new DataColumn("accountId", typeof(string)));
                
                int selectx =0;
           
                cbxAccounts.DataSource = tabl;
                cbxAccounts.DisplayMember = "accountText";
                cbxAccounts.ValueMember = "accountId";
                if (tabl.Rows.Count > 0)
                    cbxAccounts.SelectedIndex = selectx;
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接服务器出现异常，请服务器设置及网络是否正常");
            }
        
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Rebind();
        }

        private void cbxAccounts_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void LoginForm_Load(object sender, EventArgs e)
        {

        }

        private void tbxPort_TextChanged(object sender, EventArgs e)
        {

        }

    }
}